package net.zjitc.homework.homework10.phone;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author 李广彦
 * @date 2019-04-23-22：14
 */
public class PhoneTest {
    static Scanner scannerInt = new Scanner(System.in);
    static Scanner scannerString = new Scanner(System.in);
    static PhoneDao dao = new PhoneDaoDB();

    public static void main(String[] args) {
        while (true) {
            System.out.println("------欢迎光临------");
            System.out.println("1--手机上架");
            System.out.println("2--查询所有");
            System.out.println("3--删除手机");
            System.out.println("4--更改手机");
            System.out.println("5--退出程序");
            System.out.println("请输入你的选择：");
            int choose = scannerInt.nextInt();
            switch (choose) {
                case 1:
                    putUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    deleteUI();
                    break;
                case 4:
                    changeUI();
                    break;
                case 5:
                    System.out.println("已退出程序");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误！");
                    break;
            }

        }
    }

    private static void putUI() {
        System.out.println("------手机入库------");
        System.out.println("请输入手机编号：");
        String id = scannerString.nextLine();
        System.out.println("请输入手机名：");
        String name = scannerString.nextLine();
        System.out.println("请输入手机型号：");
        String type = scannerString.nextLine();
        System.out.println("请输入手机颜色：");
        String color = scannerString.nextLine();
        Phone phone = new Phone(id, name, type, color);
        Phone s = dao.selectByID(id);
        if (s == null) {
            int c = dao.insert(phone);
            if (c > 0) {
                System.out.println("添加成功！");
            } else {
                System.out.println("添加失败！");
            }
        } else {
            System.out.println("添加失败！该手机编号重复了");
        }
    }

    private static void selectUI() {
        System.out.println("------查询所有------");
        System.out.println("\t编号\t名称\t型号\t颜色");
        ArrayList<Phone> arrayList = dao.seleciAll();
        Iterator<Phone> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Phone phone = iterator.next();
            System.out.println(phone.getId() + "\t" + phone.getName() + "\t" + phone.getType() + "\t" + phone.getColor());
        }
    }

    private static void deleteUI() {
        System.out.println("------删除界面------");
        System.out.println("请输入要删除的手机编号：");
        String id = scannerString.nextLine();
        Phone phone = dao.selectByID(id);
        if (phone == null) {
            System.out.println("要删除的手机不存在");
        } else {
            int c = dao.delete(id);
            if (c > 0) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！");
            }
        }
    }

    private static void changeUI() {
        System.out.println("------更改界面------");
        System.out.println("请输入要修改的手机编号：");
        String id = scannerString.nextLine();
        Phone s = dao.selectByID(id);
        if (s == null) {
            System.out.println("你要更新的手机不存在");
        } else {
            System.out.println("\t编号\t名称\t型号\t颜色");
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getType() + "\t" + s.getColor());
            System.out.println("请输入要更改的手机名：");
            String name = scannerString.nextLine();
            System.out.println("请输入要更改的手机型号：");
            String type = scannerString.nextLine();
            System.out.println("请输入要更改的手机颜色：");
            String color = scannerString.nextLine();
            Phone phone = new Phone(id, name, type, color);
            int c = dao.update(phone);
            if (c>0){
                System.out.println("更新成功！");
            }else {
                System.out.println("更新失败！");
            }
        }
    }
}
